package com.qidian.buy.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qidian.buy.entity.Item;
import com.qidian.buy.entity.ItemDesc;
import com.qidian.buy.entity.Specifications;
import com.qidian.buy.mapper.ItemDescMapper;
import com.qidian.buy.mapper.ItemMapper;
import com.qidian.buy.mapper.SpecificationsMapper;
import com.qidian.buy.service.IItemService;
import com.qidian.buy.util.pojo.LayuiTableDTO;
import com.qidian.buy.util.pojo.QDResult;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @Author 郭浩
 */
@Service
public class ItemServiceImpl implements IItemService {
    @Resource
    private ItemMapper itemMapper;
    @Resource
    private ItemDescMapper itemDescMapper;
    @Resource
    private SpecificationsMapper specificationsMapper;
    @Override
    @Transactional
    public QDResult save(Item item, ItemDesc itemDesc) {
        item.setCreated(new Date());
        item.setUpdated(new Date());
        item.setStatus(1);
        if (itemMapper.insert(item)==1){
            itemDesc.setId(item.getId());
            itemDescMapper.insert(itemDesc);
            if(item.getSpecificationsList()!=null){
                for (Specifications spe : item.getSpecificationsList()){
                    itemMapper.saveItemAndSpecifications(item.getId(),spe.getId());
                }
            }
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    public QDResult edit(Item item) {
        if (itemMapper.updateById(item)==1){
            return QDResult.ok();
        }
        return QDResult.fal();
    }

    @Override
    public QDResult delete(Item item) {
        if (itemMapper.updateById(item)==1){
            return QDResult.ok();
        }
        return QDResult.fal();
    }
    @Override
    public QDResult<Item> get(Long itemId) {
        Item item = itemMapper.queryById(itemId);
        if (item!=null){
            return QDResult.ok(item);
        }
        return QDResult.notFound();
    }

    @Override
    public LayuiTableDTO<Item> query(int page, int limit, Item item) {
        LayuiTableDTO<Item> dto = new LayuiTableDTO<>();
        IPage<Item> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(limit);
        QueryWrapper<Item> wrapper = new QueryWrapper<>();
        // 组织查询条件
        if(item != null) {
            wrapper.like(item.getTitle()!=null && !item.getTitle().equals(""),"title", item.getTitle());
            wrapper.eq(item.getCategoryId()!=null && item.getCategoryId()!=0,"category_id",item.getCategoryId());
            wrapper.eq(item.getStatus()!=null && item.getStatus()!=0,"status",item.getStatus());
        }
        pager = itemMapper.selectPage(pager,wrapper);
        dto.setCount(pager.getTotal());
        dto.setData(pager.getRecords());
        return dto;
    }

    public QDResult search(Item item,int page,int limit){
        QueryWrapper wrapper = new QueryWrapper();
        if(item!=null){
            wrapper.like(item.getTitle()!=null && !item.getTitle().equals(""),"title",item.getTitle());
            wrapper.eq(item.getCategoryId()!=null && !item.getCategoryId().equals(0),"category_id",item.getCategoryId());
        }
        IPage<Item> pager = new Page<>();
        pager.setCurrent(page);
        pager.setSize(limit);
        pager = itemMapper.selectPage(pager,wrapper);
        return QDResult.ok(pager);
    };
}
